<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:45
 */

function mobile_service_capacity_list($parameters){
    global $user;
    $query = db_select('addr', 'A');
    $query->join('capacity', 'C', 'A.addr_id=C.addr_id');//内连接 成一个表
    $query->leftJoin('fertilizer', 'F', 'F.fertilizer_id=C.fertilizer_id');
    $query->fields('C');
    $query->addField('F', 'fertilizer_name');
    $query->addField('C', 'fertilizer_id');
    $query->addField('C', 'addr_id');
    if(!user_access('view all capacity') && user_access('view own capacity')){
        $query->condition('C.author_uid',$user->uid, '=');
    }
    //地址级别查询
    if (!empty($parameters['village'])) {//乡镇级
        $query->condition('A.village_id', $parameters['village'], '=');
    }else if (!empty($parameters['town'])) {//乡镇级
        $query->condition('A.town_id', $parameters['town'], '=');
    } elseif (!empty($parameters['county'])) {//县级
        $query->condition('A.county_id',$parameters['county'], '=');
    } elseif (!empty($parameters['city'])) {//市级
        $query->condition('A.city_id', $parameters['city'], '=');
    } elseif (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    //模糊条件筛选
    if (!empty($parameters['year'])) {//模糊年度查询条件
        $query->condition('C.year', '%' . db_like($parameters['year']) . '%', 'LIKE');
    }

    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');

    $query->orderBy('C.capacity_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('C.capacity_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $capacity_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $capacity_list[] = $result;
        }
    }
    $object_capacity_list = (object)["data"=>$capacity_list];
    return  $object_capacity_list;
}
function mobile_service_capacity_add_validate(&$parameters) {
    $empty = false;
    $errorMsg="";
    if(empty($parameters['addr_id'])){
        $errorMsg .= '必须填写 地址';
        $empty = true;
    }
    if(empty($parameters['year'])){
        $errorMsg .= '必须填写 年份.';
        $empty = true;
    }
    if(empty($parameters['fertilizer_id'])){
        $errorMsg .=  '必须填写 肥料类别.';
        $empty = true;
    }
    if(!empty($parameters['capacity'])){
        $reg = '/^\d{1,8}(.\d{0,2})?$/';
        if(!preg_match ($reg, $parameters['capacity']) || (strlen(explode('.',$parameters['capacity'])[0])>8)) {
            $errorMsg .=  "市场容量格式错误.";
        }
    }
    if(!$empty && empty($parameters['capacity_id'])){
        $address = $parameters['addr_id'];
        $query = db_select('capacity', 's');
        $query->condition('s.addr_id',$address);
        $query->condition('s.year',$parameters['year']);
        $query->condition('s.fertilizer_id',$parameters['fertilizer_id']);
        $query->fields('s');
        $record = $query->execute()->fetchAssoc();
        if(!empty($record)){
            $errorMsg .= "该地区在".$parameters['year']."年"."该肥料信息已存在.";
        }
    }
    if(!$empty && isset($parameters['capacity_id']) && $parameters['capacity_id']) {
        $address = $parameters['addr_id'];
        $query = db_select('capacity', 's');
        $query->condition('s.addr_id',$address);
        $query->condition('s.year',$parameters['year']);
        $query->condition('s.fertilizer_id',$parameters['fertilizer_id']);
        $query->condition('s.capacity_id', $parameters['capacity_id'], '!=');
        $query->fields('s');
        $record = $query->execute()->fetchAssoc();
        if(!empty($record)){
            $errorMsg .= "该地区在".$parameters['year']."年"."该肥料信息已存在";
        }
    }
    return $errorMsg;

}
function mobile_service_capacity_add($parameters){
    $errorMsg = mobile_service_capacity_add_validate($parameters);
    if(!empty($errorMsg)){
        return (object)["option" => 0,"msg" =>$errorMsg];
    }
    global $user;
    $module_name="capacity";
    $id = !empty($parameters['capacity_id'])?$parameters['capacity_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('capacity') // Table name no longer needs {}
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'fertilizer_id' => $parameters['fertilizer_id'],
            'year' => $parameters['year'],
            'capacity' => $parameters['capacity'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
        $result_option = (object)["option" => 1];
        return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('capacity')
            ->fields(array(
                'addr_id' => $parameters['addr_id'],
                'fertilizer_id' => $parameters['fertilizer_id'],
                'year' => $parameters['year'],
                'capacity' => $parameters['capacity'],
                'timestamp' => time(),))
            -> condition('capacity_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}